package cn.edu.jxau.test;

import java.util.LinkedList;

public class Main {

    public static void main(String[] args) {
        
        TwoStackQueue queue = new TwoStackQueue();
        queue.add(1);
        System.out.println(queue.peek());
        System.out.println(queue.poll());
        queue.add(2);
        queue.add(3);
        queue.add(4);
        System.out.println(queue.peek());
    }
}

/**
 * @author 付大石
 */
class TwoStackQueue {
    
    private LinkedList<Integer> pushStack;
    private LinkedList<Integer> popStack;
    
    public TwoStackQueue() {
        
        pushStack = new LinkedList<>();
        popStack = new LinkedList<>();
    }
    
    public void add(int item) {
        pushStack.push(item);
    }
    
    public int poll() {
        
        if(pushStack.isEmpty() && popStack.isEmpty()) {
            throw new RuntimeException("the queue is empty");
        } else if(popStack.isEmpty()) { //popStack为空，pushStack不为空，将pushStack中的元素一次性全部放入popStack中
            while(!pushStack.isEmpty()) {
                popStack.push(pushStack.pop());
            }
        }
        return popStack.pop();
    }
    
    public int peek() {
        
        if(pushStack.isEmpty() && popStack.isEmpty()) {
            throw new RuntimeException("the queue is empty");
        } else if(popStack.isEmpty()) { //popStack为空，pushStack不为空，将pushStack中的元素一次性全部放入popStack中
            while(!pushStack.isEmpty()) {
                popStack.push(pushStack.pop());
            }
        }
        return popStack.peek();
    }
}
